Okay, so last lesson we had a look at making a torch or a flashlight.
Okay.
And we learned about the three different events that allow you to code tools properly.
Okay.
Today we're going to have a look at sounds.
I'm also going to look at how to add sounds in onto our tool.
Okay.
Now sounds can either go of 1 or 2 places.
They can go in the workspace or they can go in the sound service.
Again we did look at that sounds early on in the course.
So if you need a refresher go look at that.
But basically, um, whichever place you put it in, whether it's a workspace or sound service, they'll
be coded to the exact same.
So just for this example, I'm going to put it into a part here and a script inside of that.
Okay.
So let's find any kind of sound.
So let's go over to audio and let's go for rain on leaves.
The first one that came up.
Copy the Roblox asset ID and let's put it into our own sound just here.
Mm.
Let's make sure it works.
Yeah, I think it might be a bit difficult for you to hear on the microphone, but I can promise you
again, it does work.
And let's reference the sound.
So local sound is equal to script dot parent.
And that's the part dot sound o dot sound okay.
So controlling sounds.
There's three pre-made kind of built in functions that you need to know.
And that's it okay.
So you have got sound colon play which plays the sound.
You have got sound colon pause which pauses the sound.
And then you have sound colon stop which stops the sound.
Okay.
So these are the three here that you would use, um, that you would use to control your sounds.
Okay.
Just remember that when you pause a sound, if you decide to play it again, it will play at the point
where you paused it.
Whereas if you stop it and then play it, it will play from the start.
So that's the difference between pause and stop.
Okay.
Um, now this is great.
For the majority of times you don't want to code a sound.
However, if you want a sound to repeat very quickly.
Um, so for example, if you're making, um, footsteps or a gun sound or something like that where
you need a sound to be played very, very quickly, it won't quite work as as it won't quite sound as
good as it should do, basically, because if you try to play the sound before the sound finishes,
it will stop immediately.
Stop immediately there, go to the start and then play.
So imagine a gun, for example.
Um, the kind of fade out sound after you shoot, the gun will be stopped for it to shoot again.
Okay.
Which is no good.
So the only other thing you need to know about this is that if you do want to play it very fast, I
would recommend cloning the sound, playing the clone, and then destroying the clone, which basically
means that you can play multiple at once.
So it will it will sound a lot better.
Okay, um.
So local you say local clone is equal to sound colon clone.
You could then say clone colon play wait however long it needs to play one second for example, and
then clone colon destroy.
Okay.
Or alternatively you could turn the play on, remove property on, and just simply add it in and destroy
it like that.
There, that could work as well.
Okay, um, I can't really show you this here because it won't really it won't pick up too well on the
microphone.
Um, but maybe try it out for yourself.
And I can assure you that it would sound a lot better.
Okay.
Um, but, yeah.
So now we know are three different sounds.
Let's go back to our flashlight and properly code this.
Okay.
Um, so the flash actually has a sound in here.
Two sounds, this one here, which is when you turn it on, and this one here when you equip it, hopefully
you can hear those.
Okay.
So I'm going to get rid of that part and all that stuff in here.
We're going to go have a look back at our flashlight.
Um so when the tool is equipped we want to play the equip sound okay.
This means obviously reference these sounds.
First I'm going to say local um equip sound is equal to um tool dot handle dot equip and then local.
Um.
Switch sound is equal to tool dot handle dot sound.
Okay.
And in here we want to play the equipped sound.
Click on play like this here on equipped sound.
We want to say equipped sound on play again.
And when it's activated we don't want to.
Oh no we could be honest.
So you might not want to play the switch sound when you turn it off.
But when you think about it, you probably do because you'll you'll still hear the click button.
So you probably would want to do both at the same time.
So switch sound, cut on play switch down cut on play.
And the reason why we don't have to clone it and do all that kind of stuff is because it's just a torch,
so you're not really going to be spamming it, pressing it on and on and on and on and on again.
Okay.
If a player wanted to, they could.
Okay.
And it might not sound the best if they do that, but the player knows that there is not something that
the player would come across normally basically.
So you can use it that way if you wanted to.
But I think for this torch example, there's not much point doing so.
Okay, let's play this out.
Let's play this and and see what happens.
So it's equipped a flashlight and our sound plays nicely.
Let's left click.
Let's left click to turn it on.
And the town turns on.
Let's turn it off and the sound turns off as well.
And we can also unequip it like that there as well.
Okay.
And yeah that is pretty much it for sounds.
Okay.
It's just the colon colon play colon pause and colon stop.
And just being aware that if you do want it to repeat a lot of times, um, you'll have to make sure
that you're cloning it.
Okay.
So hopefully you've enjoyed these last few lessons at applying our knowledge to these different areas.
Okay.
I think it's quite interesting because we've been focusing on parts for the majority of it because they're
quite easy.
But now all that knowledge you've, you know, you can just go ahead and apply to all these different
areas, which is pretty cool.
Okay.
Now we only have a few lessons left of this section of the course, which is quite exciting.
Um, we're not going to be applying our knowledge.
We're going to be learning some new we're still going to be applying our knowledge, but to new topics.
Okay.
We're going to learn about tables and how to go through tables.
Okay.
So enjoy spending some time using your previous knowledge on some of these different areas.
Okay.
And then join me in the next lesson.
And we're going to be having a look at what tables are and how to use them.